package Reflection;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.SplittableRandom;

public class Reflection {
    public static void main(String[] args) throws Exception {

//        Class stdCls = Student.class;
//        System.out.println(stdCls.getName());
//        System.out.println(stdCls.getField("score"));
//        System.out.println(stdCls.getField("name"));
//        System.out.println(stdCls.getDeclaredField("grade"));
//        Object p = new Person("xiao wang");
//        Class c = p.getClass();
//        Field f = c.getDeclaredField("name");
//        f.setAccessible(true);
//        f.set(p, "xiao ming");
//        Object value = f.get(p);
//        System.out.println(value);
//        String s = "hello world";
//        Method m = String.class.getMethod("substring", int.class);
//        Method m2 = String.class.getMethod("substring", int.class, int.class);
//        String sub = (String) m.invoke(s, 6);
//        String sub2 = (String) m2.invoke(s, 6,9);
//        System.out.println(sub);
//        System.out.println(sub2);
//        Constructor const1 = Integer.class.getConstructor(int.class);
//        Integer i = (Integer) const1.newInstance(123);
//        Constructor const2 = Integer.class.getConstructor(String.class);
//        Integer i2 = (Integer) const2.newInstance("123");
        InvocationHandler handler = new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println(method);
                if (method.getName().equals("morning")) {
                    System.out.println("Good morning, " + args[0]);
                }
                return null;
            }
        };
        Hello hello = (Hello) Proxy.newProxyInstance(
                Hello.class.getClassLoader(),// 传入ClassLoader
                new Class[]{Hello.class},// 传入要实现的接口
                handler);// 传入处理调用方法的InvocationHandler
        hello.morning("Bob");
    }
}
//动态代理这边看不懂
interface Hello {
    void morning(String name);
}

class Student extends Person {
    public int score;
    private int grade;
}

class Person {
    private String name;

    public Person(String name) {
        this.name = name;
    }

    public Person() {
    }
}